/**
 * @Company 航天科技山西公司
 * @Project hitech-WORKFLOW-Web
 * @Package com.hitech.workflow.util
 * @Title WorkFlowUtils.java
 * @Description TODO(描述)
 * @author 颜金星
 * @create 2017年5月15日-下午12:59:50
 * @version V 1.0
 */
package com.casic.modules.activiti.util;

import com.google.common.collect.Maps;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.repository.ProcessDefinition;

import java.util.List;
import java.util.Map;

/**
 * @Company 航天科技山西公司
 * @Project hitech-WORKFLOW-Web
 * @Package com.hitech.workflow.util
 * @ClassName WorkFlowUtils.java
 * @Description TODO(工作流工具类)
 * @author 颜金星
 * @create 2017年5月15日-下午12:59:50
 */
public class WorkFlowUtils {
	
	 private static Map<String, ProcessDefinition> map = Maps.newHashMap(); 
	
	 private static Map<String, List<ActivityImpl>> activities = Maps.newHashMap();

	 private static Map<String, ActivityImpl> singleActivity = Maps.newHashMap();
    /**
     * 
     * @Title getProcessDefinitionByProcDefId
     * @Description (根据procDefId获取ProcessDefinition对象)
     * @author 颜金星
     * @create 2017年5月15日-下午1:02:41
     * @Param @param procDefId
     * @Param @param repositoryService
     * @Param @return
     * @return ProcessDefinition
     * @throws
     */
	public static ProcessDefinition getProcessDefinitionByProcDefId(String procDefId,RepositoryService repositoryService){
		ProcessDefinition processDefinition=repositoryService.createProcessDefinitionQuery().processDefinitionId(procDefId).singleResult();
		put(processDefinition.getId(),processDefinition);
		return processDefinition;
	}
	/**
	 * 
	 * @Title put
	 * @Description (将流程下的节点放入map)
	 * @author 颜金星
	 * @create 2017年5月15日-下午3:49:42
	 * @Param @param processDefinitionId
	 * @Param @param processDefinition
	 * @return void
	 * @throws
	 */
	public static void put(String processDefinitionId, ProcessDefinition processDefinition) {
        map.put(processDefinitionId, processDefinition);
        ProcessDefinitionEntity pde = (ProcessDefinitionEntity) processDefinition;
        activities.put(processDefinitionId, pde.getActivities());
        for (ActivityImpl activityImpl : pde.getActivities()) {
            singleActivity.put(processDefinitionId + "_" + activityImpl.getId(), activityImpl);
        }
    }
	/**
	 * 
	 * @Title getActivityName
	 * @Description (获取节点名称)
	 * @author 颜金星
	 * @create 2017年5月15日-下午3:49:21
	 * @Param @param procDefId
	 * @Param @param activityId
	 * @Param @return
	 * @return ActivityImpl
	 * @throws
	 */
	public static ActivityImpl getActivityName(String procDefId,String activityId){
		 ActivityImpl activityImpl = singleActivity.get(procDefId + "_" + activityId);
		 return activityImpl;
	}
}
